package com.sxx.beautifulcity.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.entity.SpecialityData;
import com.sxx.beautifulcity.entity.VillageData;
import com.sxx.beautifulcity.listenerInterface.OnMyItemClickListener;

import java.util.List;

public class SpecialityAdapter extends RecyclerView.Adapter<SpecialityAdapter.SpecialityAdapterViewHolder> {

    private List<SpecialityData> list;
    private OnMyItemClickListener listener;

    public void setOnMyItemClick(OnMyItemClickListener l) {
        listener = l;
    }

    /**
     * Created by dllo on 15/10/22.
     * 特产
     */


    public void addData(List<SpecialityData> datas) {
        list = datas;
        notifyDataSetChanged();
    }

    public SpecialityAdapter(Context context) {

    }


    class SpecialityAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private SimpleDraweeView specialityDV;
        private TextView specialityNameTv;
        private RatingBar specialityRatingBar;
        private LinearLayout speciality_linearlayout;
        private int position;

        public SpecialityAdapterViewHolder(View itemView) {
            super(itemView);
            specialityDV = (SimpleDraweeView) itemView.findViewById(R.id.specialityDV);
            specialityNameTv = (TextView) itemView.findViewById(R.id.specialityNameTv);
            specialityRatingBar = (RatingBar) itemView.findViewById(R.id.specialityRatingBar);
            speciality_linearlayout = (LinearLayout) itemView.findViewById(R.id.speciality_linearlayout);
            speciality_linearlayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (list != null && list.size() > 0) {
                SpecialityData data = list.get(position);
                if (listener != null) {
                    listener.onMyItemClick(data);
                }
            }
        }
    }

    @Override
    public SpecialityAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.activity_speciality_item, null);
        return new SpecialityAdapterViewHolder(v);
    }

    @Override
    public void onBindViewHolder(SpecialityAdapterViewHolder holder, int position) {
        if (list != null && list.size() > 0) {
            SpecialityData data = list.get(position);
            holder.specialityNameTv.setText(data.getName());
            holder.specialityRatingBar.setNumStars(data.getStar());
            if (data.getImage() != null && data.getImage().length() > 0) {
                String path = data.getImage();
                frescoImage(path, holder.specialityDV);
            }
        }
        holder.position = position;

    }

    public void frescoImage(String path, SimpleDraweeView draweeView) {
        Uri uri = Uri.parse(path);
        draweeView.setImageURI(uri);
    }

    @Override
    public int getItemCount() {

        return list != null && list.size() > 0 ? list.size() : 0;
    }

}
